/**
 * WebSocket工具
 * @param opts
 * @returns {{onmessage: null, init: bean.init, survive: bean.survive, reconnect: bean.reconnect, onerror: null, myWebSocket: null, onopen: null, onclose: null, testTime: number, close: bean.close, send: bean.send, url: null}}
 * @constructor
 */
var WebSocketUtil = function (opts){
    var bean = {
        // WebSocket对象
        myWebSocket: null,
        // 地址
        url: null,
        // 测试连接是否存在间隔时间
        testTime: 10000,
        // WebSocket 事件
        onmessage: null,
        onopen: null,
        onerror: null,
        onclose: null,

        // 初始化
        init: function (){
            if (!'WebSocket' in window) {
                alert("浏览器不支持WebSocket，这将导致无法在系统使用过程中实时接收推送消息，请更换浏览器！")
                return
            }
            var thas = this;

            // 参数初始化
            if(opts){
                const optsName = Object.keys(opts);
                for (let str of optsName) {
                    console.log(str)
                    if(opts[str]){
                        thas[str] = opts[str]
                    }
                }
            }

            if(!this.url || !this.onmessage){
                console.error("WebSocket加载失败，无效的参数！")
                return
            }
            //实现化WebSocket对象，指定要连接的服务器地址与端口建立连接
            try {
                this.myWebSocket = new WebSocket(this.url);
            } catch (e) {
                console.error("WebSocket连接异常！",e)
                return
            }
            // 接收数据
            this.myWebSocket.onmessage = function(data) {
                console.log("WebSocket接到消息!");
                if(thas.onmessage){
                    thas.onmessage(data)
                }
            };
            // 初始成功
            this.myWebSocket.onopen = function(data) {
                console.log("WebSocket连接成功!");
                if(thas.onopen){
                    thas.onopen(data)
                }
            };
            // 发生异常
            this.myWebSocket.onerror = function(data) {
                console.log("WebSocket异常!")
                if(thas.onerror){
                    thas.onerror(data)
                }
            };
            // 关闭数据函数
            this.myWebSocket.onclose = function(data) {
                console.log("WebSocket关闭!");
                if(thas.onclose){
                    thas.onclose(data)
                }
            }
        },
        //关闭连接
        close: function () {
            var thas = this;
            //去掉定时器
            if(this.surviveTiming){
                window.clearInterval(thas.surviveTiming);
            }
            this.myWebSocket.close();
        },
        //发送消息
        send: function (msg) {
            this.myWebSocket.send(msg);
        },
        // 测试连接是还在
        survive: function (){
            var thas = this;
            if(!thas.surviveTiming){
                thas.surviveTiming = window.setInterval(function (){
                    if(thas.myWebSocket.readyState !== 1){
                        console.log("WebSocket断开，正在重新连接!")
                        thas.reconnect(thas.url)
                    }else {
                        console.log("WebSocket连接还存在！")
                    }
                },thas.testTime)
            }
        },
        // 重新连接
        reconnect: function (){
            var thas = this;
            if(this.myWebSocket.readyState === 1){
                console.log("WebSocket已经连接!")
                return
            }
            if(this.myWebSocket.readyState === 2){
                console.log("WebSocket正在进行关闭，1秒后重新连接!")
                setTimeout(function () {
                    thas.reconnect(thas.url);
                }, 1000);
                return
            }
            thas.init(thas.url)
        },
    }

    return (function (){
        bean.init()
        bean.survive()
        return bean;
    })()
}

/**示例
 var webSocket = new WebSocketUtil({
    url: websocket服务地址,
    onmessage: 收到服务端消息时的回调函数
});*/
